package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author 张琪
 * @date 2020/12/12
 * @describe
 */
@Service
public class UserServiceImpl implements UserService {

    public static HashMap map = new HashMap();

    static {
        map.put(1, "username");
        map.put(2, "phone");
        map.put(3, "email");
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean findInfo(String param, Integer type) {
        QueryWrapper queryWrapper = new QueryWrapper();

        /*该方法太麻烦, 使用静态的HashMap来选择数据
        switch (type){
            case 1: queryWrapper.eq("username", param);break;
            case 2: queryWrapper.eq("phone", param);break;
            case 3: queryWrapper.eq("email", param);break;
        }*/
        queryWrapper.eq(map.get(type), param);
        int rows = userMapper.selectCount(queryWrapper);
        /*
        该段代码也可以进行优化
        if(rows != 0){
            return true;
        }
        return false;*/
        return rows > 0;
    }

    @Override
    public List<User> findUserList() {

        List<User> users = userMapper.selectList(null);
        return users;
    }
}
